##########################################
# Replication Data for Proksch, Lowe, Wäckerle, Soroka. (2018). Multilingual Sentiment Analysis: A New Approach to Measuring Conflict in Legislative Speeches. Legislative Studies Quarterly, Forthcoming.
##########################################

#Part 5: Comparative Application: Sentiment in European Parliaments
##########################
# Intro
rm(list = ls())
require(quanteda)
require(tidyverse)
library(zoo)
library(rstudioapi)

current_path <- getActiveDocumentContext()$path 
setwd(dirname(current_path ))
load("auto_dictionaries_lsd.RData")
load("lsde_frenche_germane.RData")

###############################################
# Spain
###############################################

load("5_corpus_spain.RData")
#Calculate sentiment using the automatically translated sentiment dictionary
senti_spain=data.frame(dfm(corpus_spain,
                           remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_es))
#Enter Metadata
senti_spain$date=docvars(corpus_spain,"months")
senti_spain$session=docvars(corpus_spain,"session")
senti_spain$speaker=docvars(corpus_spain,"speaker")
senti_spain$party=docvars(corpus_spain,"party")
senti_spain$party.label=docvars(corpus_spain,"party.label")
senti_spain$Gov_Parties=docvars(corpus_spain,"Gov_Parties")
senti_spain$Prime_Minister=docvars(corpus_spain,"Prime_Minister")
senti_spain$Role=docvars(corpus_spain,"Role")
senti_spain$nextelection=docvars(corpus_spain,"nextelection")
senti_spain$time_to_next_election=docvars(corpus_spain,"time_to_next_election")
senti_spain$dist_to_next_election_months=docvars(corpus_spain,"dist_to_next_election_months")
senti_spain$log_dist_to_next_election_months=docvars(corpus_spain,"log_dist_to_next_election_months")
senti_spain$country_name=docvars(corpus_spain,"country_name")
senti_spain$marginality=docvars(corpus_spain,"marginality")
senti_spain$last_year_before_election=docvars(corpus_spain,"last_year_before_election")

save(senti_spain,file="5_spain_senti.RData")
rm(senti_spain,corpus_spain)
###############################################
# Finland
###############################################

load("5_corpus_finland.RData")
#Calculate sentiment using the automatically translated sentiment dictionary
senti_finland=data.frame(dfm(corpus_finland,
                           remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_fi))
#Enter Metadata
senti_finland$date=docvars(corpus_finland,"months")
senti_finland$session=docvars(corpus_finland,"session")
senti_finland$speaker=docvars(corpus_finland,"speaker")
senti_finland$party=docvars(corpus_finland,"party")
senti_finland$party.label=docvars(corpus_finland,"party.label")
senti_finland$Gov_Parties=docvars(corpus_finland,"Gov_Parties")
senti_finland$Prime_Minister=docvars(corpus_finland,"Prime_Minister")
senti_finland$Role=docvars(corpus_finland,"Role")
senti_finland$nextelection=docvars(corpus_finland,"nextelection")
senti_finland$time_to_next_election=docvars(corpus_finland,"time_to_next_election")
senti_finland$dist_to_next_election_months=docvars(corpus_finland,"dist_to_next_election_months")
senti_finland$log_dist_to_next_election_months=docvars(corpus_finland,"log_dist_to_next_election_months")
senti_finland$country_name=docvars(corpus_finland,"country_name")
senti_finland$marginality=docvars(corpus_finland,"marginality")
senti_finland$last_year_before_election=docvars(corpus_finland,"last_year_before_election")
save(senti_finland,file="5_finland_senti.RData")
rm(senti_finland,corpus_finland)

###############################################
# Germany
###############################################

load("5_corpus_germany.RData")
#Calculate sentiment using the automatically translated sentiment dictionary
senti_germany=data.frame(dfm(corpus_germany,
                             remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_de))
#Enter Metadata
senti_germany$date=docvars(corpus_germany,"months")
senti_germany$session=docvars(corpus_germany,"session")
senti_germany$speaker=docvars(corpus_germany,"speaker")
senti_germany$party=docvars(corpus_germany,"party")
senti_germany$party.label=docvars(corpus_germany,"party.label")
senti_germany$Gov_Parties=docvars(corpus_germany,"Gov_Parties")
senti_germany$Prime_Minister=docvars(corpus_germany,"Prime_Minister")
senti_germany$Role=docvars(corpus_germany,"Role")
senti_germany$nextelection=docvars(corpus_germany,"nextelection")
senti_germany$time_to_next_election=docvars(corpus_germany,"time_to_next_election")
senti_germany$dist_to_next_election_months=docvars(corpus_germany,"dist_to_next_election_months")
senti_germany$log_dist_to_next_election_months=docvars(corpus_germany,"log_dist_to_next_election_months")
senti_germany$country_name=docvars(corpus_germany,"country_name")
senti_germany$marginality=docvars(corpus_germany,"marginality")
senti_germany$last_year_before_election=docvars(corpus_germany,"last_year_before_election")
save(senti_germany,file="5_germany_senti.RData")
rm(senti_germany,corpus_germany)

###############################################
# Czech Republic
###############################################

load("5_corpus_czech.RData")
#Calculate sentiment using the automatically translated sentiment dictionary
senti_czech_republic=data.frame(dfm(corpus_czech,
                             remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_cs))
#Enter Metadata
senti_czech_republic$date=docvars(corpus_czech,"months")
senti_czech_republic$session=docvars(corpus_czech,"session")
senti_czech_republic$speaker=docvars(corpus_czech,"speaker")
senti_czech_republic$party=docvars(corpus_czech,"party")
senti_czech_republic$party.label=docvars(corpus_czech,"party.label")
senti_czech_republic$Gov_Parties=docvars(corpus_czech,"Gov_Parties")
senti_czech_republic$Prime_Minister=docvars(corpus_czech,"Prime_Minister")
senti_czech_republic$Role=docvars(corpus_czech,"Role")
senti_czech_republic$nextelection=docvars(corpus_czech,"nextelection")
senti_czech_republic$time_to_next_election=docvars(corpus_czech,"time_to_next_election")
senti_czech_republic$dist_to_next_election_months=docvars(corpus_czech,"dist_to_next_election_months")
senti_czech_republic$log_dist_to_next_election_months=docvars(corpus_czech,"log_dist_to_next_election_months")
senti_czech_republic$country_name=docvars(corpus_czech,"country_name")
senti_czech_republic$marginality=docvars(corpus_czech,"marginality")
senti_czech_republic$last_year_before_election=docvars(corpus_czech,"last_year_before_election")
save(senti_czech_republic,file="5_czech_republic_senti.RData")
rm(senti_czech_republic,corpus_czech)

###############################################
# Sweden
###############################################

load("5_corpus_sweden.RData")
#Calculate sentiment using the automatically translated sentiment dictionary
senti_sweden=data.frame(dfm(corpus_sweden,
                             remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_sv))
#Enter Metadata
senti_sweden$date=docvars(corpus_sweden,"months")
senti_sweden$session=docvars(corpus_sweden,"session")
senti_sweden$speaker=docvars(corpus_sweden,"speaker")
senti_sweden$party=docvars(corpus_sweden,"party")
senti_sweden$party.label=docvars(corpus_sweden,"party.label")
senti_sweden$Gov_Parties=docvars(corpus_sweden,"Gov_Parties")
senti_sweden$Prime_Minister=docvars(corpus_sweden,"Prime_Minister")
senti_sweden$Role=docvars(corpus_sweden,"Role")
senti_sweden$nextelection=docvars(corpus_sweden,"nextelection")
senti_sweden$time_to_next_election=docvars(corpus_sweden,"time_to_next_election")
senti_sweden$dist_to_next_election_months=docvars(corpus_sweden,"dist_to_next_election_months")
senti_sweden$log_dist_to_next_election_months=docvars(corpus_sweden,"log_dist_to_next_election_months")
senti_sweden$country_name=docvars(corpus_sweden,"country_name")
senti_sweden$marginality=docvars(corpus_sweden,"marginality")
senti_sweden$last_year_before_election=docvars(corpus_sweden,"last_year_before_election")
save(senti_sweden,file="5_sweden_senti.RData")
rm(senti_sweden,corpus_sweden)

###############################################
# Netherlands
###############################################

load("5_corpus_netherlands.RData")
#Calculate sentiment using the automatically translated sentiment dictionary
senti_netherlands=data.frame(dfm(corpus_netherlands,
                            remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_nl))
#Enter Metadata
senti_netherlands$date=docvars(corpus_netherlands,"months")
senti_netherlands$session=docvars(corpus_netherlands,"session")
senti_netherlands$speaker=docvars(corpus_netherlands,"speaker")
senti_netherlands$party=docvars(corpus_netherlands,"party")
senti_netherlands$party.label=docvars(corpus_netherlands,"party.label")
senti_netherlands$Gov_Parties=docvars(corpus_netherlands,"Gov_Parties")
senti_netherlands$Prime_Minister=docvars(corpus_netherlands,"Prime_Minister")
senti_netherlands$Role=docvars(corpus_netherlands,"Role")
senti_netherlands$nextelection=docvars(corpus_netherlands,"nextelection")
senti_netherlands$time_to_next_election=docvars(corpus_netherlands,"time_to_next_election")
senti_netherlands$dist_to_next_election_months=docvars(corpus_netherlands,"dist_to_next_election_months")
senti_netherlands$log_dist_to_next_election_months=docvars(corpus_netherlands,"log_dist_to_next_election_months")
senti_netherlands$country_name=docvars(corpus_netherlands,"country_name")
senti_netherlands$marginality=docvars(corpus_netherlands,"marginality")
senti_netherlands$last_year_before_election=docvars(corpus_netherlands,"last_year_before_election")
save(senti_netherlands,file="5_netherlands_senti.RData")
rm(senti_netherlands,corpus_netherlands)

###############################################
# United Kingdom
###############################################

load("5_corpus_uk.RData")
#Calculate sentiment using the extended sentiment dictionary
senti_uk=data.frame(dfm(corpus_uk,
                                 remove_punct = TRUE,remove_numbers=TRUE, dictionary = extendeddict_en))
#Enter Metadata
senti_uk$date=docvars(corpus_uk,"months")
senti_uk$session=docvars(corpus_uk,"session")
senti_uk$speaker=docvars(corpus_uk,"speaker")
senti_uk$party=docvars(corpus_uk,"party")
senti_uk$party.label=docvars(corpus_uk,"party.label")
senti_uk$Gov_Parties=docvars(corpus_uk,"Gov_Parties")
senti_uk$Prime_Minister=docvars(corpus_uk,"Prime_Minister")
senti_uk$Role=docvars(corpus_uk,"Role")
senti_uk$nextelection=docvars(corpus_uk,"nextelection")
senti_uk$time_to_next_election=docvars(corpus_uk,"time_to_next_election")
senti_uk$dist_to_next_election_months=docvars(corpus_uk,"dist_to_next_election_months")
senti_uk$log_dist_to_next_election_months=docvars(corpus_uk,"log_dist_to_next_election_months")
senti_uk$country_name=docvars(corpus_uk,"country_name")
senti_uk$marginality=docvars(corpus_uk,"marginality")
senti_uk$last_year_before_election=docvars(corpus_uk,"last_year_before_election")
save(senti_uk,file="5_uk_senti.RData")
rm(senti_uk,corpus_uk)
